﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using GroupChat2013.Service;
using GroupChat2013.Entity;
using GroupChat2013.Interface;
using GroupChat2013.Common;

namespace GroupChat2013.Logic.Message
{
    /// <summary>
    /// 群聊天信息发送接受类
    /// </summary>
    public class GroupMessage : IMessage
    {
        private ServiceUrlProxy m_ServiceUrlProxy;
        private string groupId;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userInfo">当前用户信息</param>
        /// <param name="groupId">群ID</param>
        public GroupMessage(UserInfo userInfo, string groupId)
        {
            this.groupId = groupId;
            m_ServiceUrlProxy = ServiceUrlProxy.GetInstance();
            m_ImageUpload = new GroupImageUpload(userInfo, groupId);
        }

        /// <summary>
        /// 接收聊天信息
        /// </summary>
        /// <param name="cid">cid</param>
        /// <returns>接收到的信息</returns>
        public override List<MessageInfo> ReceiverMessage(string cid)
        {
            List<MessageInfo> messageList = m_ServiceUrlProxy.GetGroupMessage(groupId, cid);
            //将json格式的content解析成对象
            ParseMessageContent(messageList);
            return messageList;
        }

        /// <summary>
        /// 发送聊天信息
        /// </summary>
        /// <param name="message">聊天信息</param>
        /// <param name="imageList">信息中的图片</param>
        /// <param name="cid">cid</param>
        /// <returns>接收到的聊天信息</returns>
        public override List<MessageInfo> SendMessage(MessageContent message, List<string> imageList, string cid)
        {
            m_ImageUpload.ImageUpload(imageList);
            message.Content = HttpUtility.UrlEncode(message.Content);
            string jsonMsg = JsonConvert.ConvertToJson(message);
            List<MessageInfo> messageList = m_ServiceUrlProxy.SendGroupMessage(groupId, jsonMsg, cid);

            //将json格式的content解析成对象
            ParseMessageContent(messageList);

            //解码
            message.Content = HttpUtility.UrlDecode(message.Content);
            return messageList;
        }
    }
}
